package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfoVo;
import com.itheima.pojo.OrderSetting;
import com.mchange.v1.db.sql.ConnectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 类 名: OrderServiceImpl
 * 描 述:
 * 作 者: 蔡畅
 * 创 建：2019年11月26日
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

//1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
//
//2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
//
//3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
//
//4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
//
//5、预约成功，更新当日的已预约人数


    @Autowired
    OrderSettingDao orderSettingDao;
    @Autowired
    OrderDao orderDao;
    @Autowired
    MemberDao memberDao;


    @Override
    public Result submit(OrderInfoVo orderInfoVo) {
        Date orderDate = orderInfoVo.getOrderDate();
        String telephone = orderInfoVo.getTelephone();
        String idCard = orderInfoVo.getIdCard();
        String name = orderInfoVo.getName();
        Integer sex = orderInfoVo.getSex();
        Integer setmealId = orderInfoVo.getSetmealId();
        String orderType = orderInfoVo.getOrderType();


        //1.根据预约时间擦寻是否存在预约设置(返回一个预约设置对象)
        OrderSetting orderSetting = orderSettingDao.findByDate(orderDate);
        if (orderSetting == null) {
            return Result.error("没有档期");

        }
        //2.检验预约是否已满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        //线程1 100 99 0
        //线程2 100 99 0
        //线程3 100 99 0
        //线程4 100 99 0
        if (number < reservations) {
            return Result.error("预约已满");
        }

        //3.根据手机号码查询是否是我们的会员
        Member member = memberDao.findByTelephone(telephone);
        //4.如果不是就自动注册
        if (member == null) {
            member = new Member();//注意:空对象要初始化
            member.setPhoneNumber(telephone);
            member.setIdCard(idCard);
            member.setSex(String.valueOf(sex));
            member.setName(name);
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        Integer memberId = member.getId();
        //5.根据会员id,套餐id,预约时间查询是否重复预约
        Order order = new Order();
        order.setSetmealId(setmealId);
        order.setOrderDate(orderDate);
        order.setMemberId(memberId);
        List<Order> orders = orderDao.findByCondition(order);
        if (CollectionUtil.isNotEmpty(orders)) {
            return Result.error("请勿重复预约");
        }
        //6.直接下单
        Order order1 = new Order();
        order1.setMemberId(memberId);
        order1.setOrderDate(orderDate);
        order1.setOrderType(orderType);
        order1.setOrderStatus(Order.ORDERSTATUS_NO);
        order1.setSetmealId(setmealId);
        orderDao.add(order1);
        //7.修改已预约人数+1
        orderSetting.setReservations(orderSetting.getReservations()+1);
        int i = orderSettingDao.editReservations(orderSetting);
        if (i == 0) {
            //没有更新成功
            throw new RuntimeException("更新失败");
        }

        //8 、返回预约成功(页面需要订单id，那就把order对象返回)
        return Result.success("",order);
    }
    @Override
    public Map findById(Integer id) {
        return orderDao.findByIdDetail(id);
    }
}
